<?php
/**
 * 周舟 hzboye010@163.com
 * addby sublime snippets
 */
namespace Common\Api;
use Common\Api\Api;
use Think\Model;
use Common\Model\ExchangeLogModel;
// use Admin\Model\MessageModel;
// use Admin\Model\MessageBoxModel;
class ExchangeLogApi extends Api{
  // const PAGE  = "Common/ExchangeLog/getPageLogsByUid";
  const QUERY    = "Common/ExchangeLog/myQuery";
  const MY_QUERY = "Common/ExchangeLog/myFind";
  const PAGE     = "Common/ExchangeLog/myPage";
  const SAVE     = "Common/ExchangeLog/save";
  // const PAGE     = "Common/ExchangeLog/query";
  const ADD      = "Common/ExchangeLog/add";
  const DELETE   = "Common/ExchangeLog/upStatus";
  const SAVEBYID = "Common/ExchangeLog/saveByID";
  const PAYBACK  = "Common/ExchangeLog/payBack";
  // const DELETE   = "Common/ExchangeLog/delete";
  const GET_INFO = "Common/ExchangeLog/getInfo";
  const COUNT    = "Common/ExchangeLog/count";

  public function myPage($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false) {
    $query = $this->model-> alias('e')->join('common_member m on e.uid=m.uid');
    if(!is_null($map)){
      $query = $query->where($map);
    }
    if(!($order === false)){
      $query = $query->order($order);
    }
    if(!($fields === false)){
      $query = $query->field('e.'.$fields.',m.nickname');
    }
    $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();
    if ($list === false) {
      $error = $this -> model -> getDbError();
      return $this -> apiReturnErr($error);
    }

    $count = $this -> model -> where($map) -> count();
    // 查询满足要求的总记录数
    $Page = new \Think\Page($count, $page['size']);

    //分页跳转的时候保证查询条件
    if ($params !== false) {
      foreach ($params as $key => $val) {
        $Page -> parameter[$key] = urlencode($val);
      }
    }

    // 实例化分页类 传入总记录数和每页显示的记录数
    $show = $Page -> show();

    return $this -> apiReturnSuc(array("show" => $show, "list" => $list));
  }

  public function payBack($code){
    $r = $this-> model ->where(array('pay_code'=>$code))
      ->setField(array('exchange_status'=>0,'pay_status'=>1));
    if($r){
    //   //   $this->redirect('Webview/ExchangeLog/payBack',array('info'=>'支付成功'));
      return true;
    }else{
      return false;
      // //   $this->redirect('Webview/ExchangeLog/payBack',array('info'=>'支付失败'));
    }
  }

  public function myFind($uid,$start,$length,$order="exchange_time desc"){
    $where   = array('l.uid'=>$uid,'l.exchange_status'=>array('gt','-2'));
    $r = $this-> model-> alias('l')
    ->join(C('DB_PREFIX').'scoregoods s on l.pid=s.id')->field('l.*,s.main_img')
    -> where($where)->limit($start,$length)->order($order)->select();
    if($r){
      foreach ($r as $k => $v) {
        $r[$k]['exchange_time'] = date('Y.m.d h:i:s',$v['exchange_time']);
        $s = intval($v['exchange_status']);
        if($s     === 0  ) $r[$k]['exchange_status'] = '待发货';
        elseif($s === -1 ) $r[$k]['exchange_status'] = '请操作';
        elseif($s === -2 ) $r[$k]['exchange_status'] = '已删除';
        elseif($s === 1  ) $r[$k]['exchange_status'] = '已发货';
      }
    }
    return $this -> apiReturnSuc($r);
  }

  public function myQuery($uid,$start,$length,$order="exchange_time desc"){
    $where   = array('uid'=>$uid,'exchange_status'=>array('gt','-2'));
    $r = $this-> model-> where($where)->limit($start,$length)->order($order)->select();
    if(false === $r) return $this->apiReturnErr($r);
    if($r){
      foreach ($r as &$v) {
        $v['exchange_time'] = date('Y.m.d h:i:s',$v['exchange_time']);
        $s = intval($v['exchange_status']);
        if($s     ===  0 ) $v['exchange_status'] = '待发货';
        elseif($s === -1 ) $v['exchange_status'] = '请操作';
        elseif($s === -2 ) $v['exchange_status'] = '已删除';
        elseif($s ===  1 ) $v['exchange_status'] = '已发货';
      }
      return $this -> apiReturnSuc($r);
    }else{
      return $this->apiReturnSuc(array());
    }
  }

  /**
   * 将兑换记录 exchange_status 设为 -2，前台不显示
   * @param  [type] $id [description]
   * @return [type]     [description]
   */
  public function upStatus($id){
    //,'exchange_status'=>1
    $r = $this-> model ->where(array('id'=>$id)) ->setField('exchange_status','-2');
    if($r){
      return $this->apiReturnSuc($r);
    }else{
      return $this->apiReturnErr($r);
    }
  }
	//初始化
  protected function _init(){
  	// $this -> type   = 6055 ;//中奖消息
  	$this -> model  = new ExchangeLogModel();
		// $this -> msg    = new MessageModel();
		// $this -> msgBox = new MessageBoxModel();
  }

  // public function myFind($uid,$page,$psize){
  //   $where   = array('uid'=>$uid);
  //   $psize   = $psize ? intval($psize):10;
  //   $count   = $this -> model -> where($where) -> count();
  //   $maxPage = ceil($count/$psize);
  //   $page    = ($page>0) ? intval($page):1;
  //   $page    = ($page<$maxPage) ? $page:$maxPage;
  //   $start   = ($page-1)*$psize;
  //   $r = $this-> model->where($where)->limit($start,$psize)->order('exchange_time desc')->select();
  //   return $this -> apiReturnSuc($r);
  // }

  // public function getPageLogsByUid($uid){
  // 	$where = array('b.to_id'=>$uid);
  //   $page = array('curpage'=>0,'size'=>10);

  // 	$r = $this->model->table(C('DB_PREFIX').'message m')
  //   	->join(C('DB_PREFIX').'message_box b ON b.msg_id = m.id')
  //   	->where($where)
  //     ->field('m.*,b.msg_status');
  //   $list = $r -> page($page['curpage'] . ',' . $page['size']) -> select();
  //   dump($list);exit;
		// if ($list === false) {
		// 	$error = $this -> model -> getDbError();
		// 	return $this -> apiReturnErr($error);
		// }
		// $count = $this -> model -> where($where) -> count();
		// $Page = new \Think\Page($count, $page['size']);
		// //分页跳转的时候保证查询条件
		// if ($params !== false) {
		// 	foreach ($params as $key => $val) {
		// 		$Page -> parameter[$key] = urlencode($val);
		// 	}
		// }
		// $show = $Page -> show();
		// return $this -> apiReturnSuc(array("show" => $show, "list" => $list));
  // }
}